<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.4/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户帐号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="code" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <a class="layui-btn" lay-submit="" lay-filter="data-search-btn">搜索</a>
                        </div>
                        <div class="layui-inline" style="float: right">
                            <a href="/admin/users/user/add" class="layui-btn layui-btn-normal">新增用户</a>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="users" lay-filter="user"></table>

        <script type="text/html" id="userBar">
            <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>

        <script type="text/html" id="checkboxTpl">
            <input type="checkbox" value="{{d.id}}" name="isLocked" lay-skin="switch" lay-filter="locked"
                   lay-text="已锁定|未锁定"
                   title="锁定" {{ d.isLocked== 1 ? 'checked' : '' }}>
        </script>
    </div>
</div>
</body>
<script src="/lib/layui-v2.5.4/layui.js" charset="utf-8"></script>
<script th:inline="javascript">
    /*<![CDATA[*/
    layui.use(['table', 'form', 'element'], function () {
        var table = layui.table,
            form = layui.form,
            $ = layui.jquery,
            element = layui.element;
        // 表格
        table.render({
            elem: '#users',
            url: '/api/admin/users/user/list',
            page: true,
            cols: [
                [
                    {field: 'id', title: 'ID', align: 'center', sort: true, hide: true},
                    {field: 'code', title: '帐号', align: 'center'},
                    {field: 'name', title: '昵称', align: 'center'},
                    {
                        field: 'sex', title: '性别', align: 'center', width: 60, templet: function (item) {
                        if (item.sex == '0') {
                            return "女";
                        } else if (item.sex == '1') {
                            return "男";
                        } else {
                            return "";
                        }
                    }
                    },
                    {
                        field: 'birthData',
                        title: '出生日期',
                        align: 'center',
                        templet: '<div>{{ layui.util.toDateString(d.birthData, "yyyy-MM-dd ") }}</div>'
                    },
                    {field: 'email', title: '电子邮箱', align: 'center'},
                    {field: 'isLocked', title: '是否锁定', templet: '#checkboxTpl', unresize: true, align: 'center'},
                    {field: 'loginCount', title: '登录次数', align: 'center', width: 90},
                    {fixed: 'right', title: '操作', toolbar: '#userBar', align: 'center', minWidth: 200}
                ]
            ]
        });

        //监听工具条
        table.on('tool(user)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'detail') { //查看
                window.location = "/admin/users/user/view?id=" + data.id;
            } else if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', function (index) {
                    //向服务端发送删除指令
                    var param = {
                        "id": data.id
                    }
                    $.ajax({
                        url: "/api/admin/users/user/confirm/delete/" + data.id,
                        type: "DELETE",
                        data: param,
                        success: function (rst) {
                            layer.msg(rst.msg);
                            if (rst.code === '1') {
                                setTimeout(function () {
                                    location.href = "/admin/users/user/list";
                                }, 2000);
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (layEvent === 'edit') { //编辑
                window.location = "/admin/users/user/edit?id=" + data.id;
            }
        });
        //监听是否锁定
        form.on('switch(locked)', function (data) {
            var param = {
                "id": data.value,
                "isLocked": this.checked
            }
            $.ajax({
                url: "/api/admin/users/user/confirm/edit/locked",
                type: "PUT",
                data: param,
                success: function (rst) {
                    layer.msg(rst.msg);
                }
            });
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('users', {
                page: {
                    curr: 1
                }, where: data.field
            }, 'data');
            return false;
        });
    });
    /*]]>*/
</script>
</html>